package com.atguigu.dga.score.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.assess.bean.GovernanceAssessDetail;
import com.atguigu.dga.assess.service.GovernanceAssessDetailService;
import com.atguigu.dga.score.bean.GovernanceAssessGlobal;
import com.atguigu.dga.score.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.score.service.CalScoreService;
import com.atguigu.dga.score.service.GovernanceAssessGlobalService;
import com.atguigu.dga.score.service.GovernanceAssessTableService;
import com.atguigu.dga.score.service.GovernanceAssessTecOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/governance")
public class ScoreController
{

    @Autowired
    private GovernanceAssessTecOwnerService ownerService;
    @Autowired
    private GovernanceAssessGlobalService globalService;

    @Autowired
    private GovernanceAssessDetailService detailService;
    /*
        返回全局分数
            {  "assessDate":"2023-04-01" ,"sumScore":90, "scoreList":[20,40,34,55,66] }
                规范，存储，计算，质量，安全
     */
    @RequestMapping("/globalScore")
    public Object getGlobalScore(){

        QueryWrapper<GovernanceAssessGlobal> queryWrapper = new QueryWrapper<GovernanceAssessGlobal>()
                .last(" where assess_date = ( select max(assess_date) from  governance_assess_global ) ");
        //需要获取当前最近的考评日期
        GovernanceAssessGlobal governanceAssessGlobal = globalService.getOne(queryWrapper);

        //{} ： 准备Map
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("assessDate",governanceAssessGlobal.getAssessDate());
        jsonObject.put("sumScore",governanceAssessGlobal.getScore());
        jsonObject.put("scoreList", Arrays.asList(
                governanceAssessGlobal.getScoreSpec(),
                governanceAssessGlobal.getScoreStorage(),
                governanceAssessGlobal.getScoreCalc(),
                governanceAssessGlobal.getScoreQuality(),
                governanceAssessGlobal.getScoreSecurity()
        ));
        return jsonObject;
    }
    /*
        问题列表
     */
    @GetMapping("/problemList/{governType}/{pageNo}/{pageSize}")
    public Object getProblemList(@PathVariable("governType") String type,
                                 @PathVariable("pageNo") Integer pageNo,
                                 @PathVariable("pageSize") Integer pageSize){

        //sql:  limit from,pageSize
        int from = (pageNo - 1) * pageSize;

        QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
                .eq("governance_type", type)
                //出了问题
                .lt("assess_score", 10)
                .last(" and assess_date = ( select max(assess_date) from  governance_assess_detail ) " +
                        " order by id limit " + from + "," + pageSize);

        List<GovernanceAssessDetail> details = detailService.list(queryWrapper);

        return details;

    }

    /*
        /governance/problemNum
            分组。
            {"SPEC":1, "STORAGE":4,"CALC":12,"QUALITY":34,"SECURITY":12}

     */
    @GetMapping("/problemNum")
    public Object getProblemNum(){

        //求最近的日期
        String assessDate = detailService.getOne(new QueryWrapper<GovernanceAssessDetail>().select(" max(assess_date) assess_date"))
                .getAssessDate();

        /*
            [
              { "governance_type":"SPEC"  , "num":2  },
              { "governance_type":"STORAGE"  , "num":4  },
              {},
              ...
            ]
         */
        List<Map<String, Object>> maps = detailService.
                listMaps(
                        new QueryWrapper<GovernanceAssessDetail>()
                                .select("governance_type", "count(*) num")
                                .eq("assess_date", assessDate)
                                .lt("assess_score", 10)
                                .groupBy("governance_type")
                );

        JSONObject jsonObject = new JSONObject();
        maps.stream()
                .forEach( map -> jsonObject.put((String)map.get("governance_type"),map.get("num")) );

        return  jsonObject;
    }

    /*
        技术人员分数排名
     */
    @GetMapping("/rankList")
    public Object getRankList(){
        //求最近的日期
        String assessDate = ownerService.getOne(new QueryWrapper<GovernanceAssessTecOwner>().select(" max(assess_date) assess_date"))
                .getAssessDate();

        /*
            [
            {"tecOwner":"zhang3" ,"score":99},
            {"tecOwner":"li4" ,"score":98},
            {"tecOwner": "wang5","score":97}   ]
         */
        List<Map<String, Object>> result = ownerService.listMaps(
                new QueryWrapper<GovernanceAssessTecOwner>()
                        .eq("assess_date", assessDate)
                        .select("tec_owner tecOwner", "score")
                        .orderByDesc("score")
        );

        return result;

    }

    @Autowired
    private CalScoreService calScoreService;
    @PostMapping("/assess/{date}")
    public Object assess(@PathVariable("date")String date) throws Exception {

        calScoreService.calScore(date);

        return "success";
    }
}
